package org.tuoren.system.controller.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaIgnore;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.tuoren.common.core.domain.R;
import org.tuoren.common.core.validate.AddGroup;
import org.tuoren.common.core.validate.EditGroup;
import org.tuoren.common.excel.utils.ExcelUtil;
import org.tuoren.common.idempotent.annotation.RepeatSubmit;
import org.tuoren.common.log.annotation.Log;
import org.tuoren.common.log.annotation.Visit;
import org.tuoren.common.log.enums.BusinessType;
import org.tuoren.common.mybatis.core.page.PageQuery;
import org.tuoren.common.mybatis.core.page.TableDataInfo;
import org.tuoren.common.web.core.BaseController;
import org.tuoren.system.domain.bo.SysVisitBo;
import org.tuoren.system.domain.vo.SysVisitVo;
import org.tuoren.system.service.ISysVisitService;

import java.util.List;

/**
 * 访问记录
 *
 * @author Lion Li
 * @date 2025-03-19
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/visit")
public class SysVisitController extends BaseController {

    private final ISysVisitService sysVisitService;

    /**
     * 查询访问记录列表
     */
    @SaCheckPermission("system:visit:list")
    @GetMapping("/list")
    public TableDataInfo<SysVisitVo> list(SysVisitBo bo, PageQuery pageQuery) {
        return sysVisitService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出访问记录列表
     */
    @SaCheckPermission("system:visit:export")
    @Log(title = "访问记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysVisitBo bo, HttpServletResponse response) {
        List<SysVisitVo> list = sysVisitService.queryList(bo);
        ExcelUtil.exportExcel(list, "访问记录", SysVisitVo.class, response);
    }

    /**
     * 获取访问记录详细信息
     *
     * @param visitId 主键
     */
    @SaCheckPermission("system:visit:query")
    @GetMapping("/{visitId}")
    public R<SysVisitVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long visitId) {
        return R.ok(sysVisitService.queryById(visitId));
    }

    /**
     * 新增访问记录
     */
    @SaCheckPermission("system:visit:add")
    @Log(title = "访问记录", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysVisitBo bo) {
        return toAjax(sysVisitService.insertByBo(bo));
    }

    /**
     * 修改访问记录
     */
    @SaCheckPermission("system:visit:edit")
    @Log(title = "访问记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/edit")
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysVisitBo bo) {
        return toAjax(sysVisitService.updateByBo(bo));
    }

    /**
     * 删除访问记录
     *
     * @param visitIds 主键串
     */
    @SaCheckPermission("system:visit:remove")
    @Log(title = "访问记录", businessType = BusinessType.DELETE)
    @PostMapping("/{visitIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] visitIds) {
        return toAjax(sysVisitService.deleteWithValidByIds(List.of(visitIds), true));
    }

    /**
     * 官网前台-访问记录埋点接口
     * @param visitType 操作类别（0其它 1后台用户 2手机端用户）
     * @param costTime 停留时间
     */
    @SaIgnore
    @PostMapping("/point")
    @Visit
    public  R<Void>  maidian(String visitType, Long costTime ) {
        return R.ok();
    }
}
